/*
	Visp is an opensource visual performance application built with Adobe® AIR™.
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
	
	Mike Creighton can be found at http://www.mikecreighton.com	
*/
package com.visp.render
{
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import com.visp.constants.OutputSize;
	
	public class Layer {
		
		private var $renderer : IRenderable;
		private var $final: BitmapData;
		private var $copyRect : Rectangle;
		private var $isEmpty : Boolean;
		
		public function Layer() {
			this.$final = new BitmapData(OutputSize.WIDTH, OutputSize.HEIGHT, true, 0x00000000);
			this.$copyRect = new Rectangle(0, 0, OutputSize.WIDTH, OutputSize.HEIGHT);
			this.removeRenderer();
		}
		
		public function nullifyRenderer() : void {
			this.$renderer = null;
			this.$isEmpty = true;
		}
		
		public function removeRenderer() : void {
			if(this.$renderer != null)
				this.$renderer.destroy();
			this.$isEmpty = true;
		}
		
		public function setRenderer(renderer : IRenderable) : void {
			this.$isEmpty = false;
			this.$renderer = renderer;
		}
		
		public function getRenderer() : IRenderable
		{
			return this.$renderer;
		}
		
		public function render() : BitmapData {
			this.$final.lock();
			
			// Clear out the bitmap that gets put on the screen
			this.$final.fillRect(this.$copyRect, 0x00000000);
			this.$final.draw(this.$renderer, null, null, null, this.$copyRect, false);
			
			this.$final.unlock();
			return this.$final;
		}
		
		public function isEmpty() : Boolean {
			return this.$isEmpty;
		}
		
		public function get outputBitmap() : BitmapData {
			return this.$final;
		}
	}
	
}